//
//  DDonateViewController.m
//  dance
//
//  Created by aether on 2023/10/20.
//

#import "DDonateViewController.h"

@interface DDonateViewController ()

@property (nonatomic, strong) IBOutlet UIView *bg;
@property (nonatomic, weak) IBOutlet UIView *inputBg;
@property (nonatomic, weak) IBOutlet UIView *loginBg;
@property (nonatomic, weak) IBOutlet UILabel *timeLabel;
@property (nonatomic, weak) IBOutlet UILabel *remindLabel;
@property (strong, nonatomic) UIDatePicker *datePicker;


@end

@implementation DDonateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    [self initViews];
}

- (void) initViews {
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame) + 20, CGRectGetHeight(self.view.frame));
    gl.startPoint = CGPointMake(0.5, 0);
    gl.endPoint = CGPointMake(0.5, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:71/255.0 green:0/255.0 blue:233/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:51/255.0 green:149/255.0 blue:247/255.0 alpha:0.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    [self.bg.layer addSublayer:gl];
    
    CAGradientLayer *gl1 = [CAGradientLayer layer];
    gl1.frame = CGRectMake(0, 0, CGRectGetWidth(self.loginBg.frame) + 20, CGRectGetHeight(self.loginBg.frame));
    gl1.startPoint = CGPointMake(0, 0.5);
    gl1.endPoint = CGPointMake(1, 0.5);
    gl1.colors = @[(__bridge id)[UIColor colorWithRed:1/255.0 green:140/255.0 blue:255/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:68/255.0 green:57/255.0 blue:229/255.0 alpha:1.0].CGColor];
    gl1.locations = @[@(0), @(1.0f)];
    self.loginBg.layer.cornerRadius = 7;
    self.loginBg.layer.masksToBounds = YES;
    [self.loginBg.layer addSublayer:gl1];
    
    
    CAGradientLayer *gl2 = [CAGradientLayer layer];
    gl2.frame = CGRectMake(0, 0, CGRectGetWidth(self.inputBg.frame) + 20, CGRectGetHeight(self.inputBg.frame));
    gl2.startPoint = CGPointMake(0, 0.5);
    gl2.endPoint = CGPointMake(1, 0.5);
    gl2.colors = @[(__bridge id)[UIColor colorWithRed:176/255.0 green:240/255.0 blue:67/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:68/255.0 green:57/255.0 blue:229/255.0 alpha:1.0].CGColor];
    gl1.locations = @[@(0), @(1.0f)];
    self.inputBg.layer.cornerRadius = 7;
    self.inputBg.layer.masksToBounds = YES;
    [self.inputBg.layer addSublayer:gl2];
    
    
    // 创建时间选择器
    self.datePicker = [[UIDatePicker alloc] init];
    self.datePicker.datePickerMode = UIDatePickerModeTime; // 设置为时间选择模式
    [self.datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    // 设置时间选择器位置
    self.datePicker.frame = CGRectMake(0, 100, self.view.bounds.size.width - 40, 220);
    
    [self.view addSubview:self.datePicker];
}

- (void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self initViews];
}

- (void)datePickerValueChanged:(UIDatePicker *)datePicker {
    NSDate *selectedDate = datePicker.date;
    
    // 创建日期格式化器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm a"]; // 12小时制时间格式
        
    // 在这里可以使用用户选择的时间进行其他操作
    int minimumIntervalInWeeks = 8; // 替换为最小间隔时间
            
    NSDate *nextDonationDate = [self calculateNextBloodDonationDateWithLastDonationDate:selectedDate minimumIntervalInWeeks:minimumIntervalInWeeks];
    
    NSString *formattedDate = [dateFormatter stringFromDate:nextDonationDate];
    NSLog(@"下一次最早的献血日期: %@", formattedDate);

    [self.remindLabel setText:[NSString stringWithFormat:@"下一次最早的献血日期: %@", formattedDate]];
}

#pragma mark - 内部方法

- (NSDate *)calculateNextBloodDonationDateWithLastDonationDate:(NSDate *)lastDonationDate minimumIntervalInWeeks:(int)minimumIntervalInWeeks {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 计算最小间隔时间的日期组件
    NSDateComponents *minimumIntervalComponents = [[NSDateComponents alloc] init];
    [minimumIntervalComponents setWeekOfYear:minimumIntervalInWeeks];
    
    // 使用日历计算下一次最早的献血日期
    NSDate *nextDonationDate = [calendar dateByAddingComponents:minimumIntervalComponents toDate:lastDonationDate options:0];
    
    return nextDonationDate;
}


@end
